


-- constants

-- Constant: JUMPING
-- Motion constant corresponding to jumping.
JUMPING = 'jumping'

-- Constant: WALKING
-- Motion constant corresponding to walking.
WALKING = 'walking'

-- Constant: STOPPED
-- Motion constant corresponding to stopped.
STOPPED = 'walking'

-- Class: Player

Player = Animation:extend
{
	width = 48,
	height = 48,

	image = 'flea-player.png',

	-- direction cannot be wholly derived from velocity.x...
	direction = RIGHT,

	motion = STOPPED,


	sequences =
	{
		pause_left  = { frames = { 1 }, fps = 6 },
		pause_right = { frames = { 5 }, fps = 6 },
		walk_left   = { frames = {1,2}, fps = 6 },
		walk_right  = { frames = {5,6}, fps = 6 },
		jump_left   = { frames = {3,4}, fps = 6 },
		jump_right  = { frames = {7,8}, fps = 6 }
	},

	new = function (self, obj)
		obj = obj or {}
		self:extend(obj)
		self:play('pause_right')
		self.direction = RIGHT
		if obj.onNew then obj:onNew() end
		return obj
	end,

	reinit = function (self)
		self:play('pause_right')
		self.direction = RIGHT
		self.direction = RIGHT
		self.motion = STOPPED
		self.x = 0
		self.y = 600 - 48
	end,


	onUpdate = function (self)


		-- Boundary (horizontal)

		if self.x < 0 then
			self.x = 0
		elseif self.x > 800 - 48 then
			self.x = 800 - 48
		end


		-- JUMPING ==> STATIONARY
		if self.motion == JUMPING and self.y >= 600 - 48 then
			self:jumpStop()
			self.y = 600 - 48
		end

		-- (WALKING|STOPPED) ==> (WALKING|JUMPING)
		if self.motion ~= JUMPING then

			if the.keys:pressed("left") then
				self:walkLeft()
			elseif the.keys:pressed("right") then
				self:walkRight()
			end

			-- We can detect more than one key press per frame
			-- if we don't group them within the same if-elseif
			-- statement.
			if the.keys:pressed(" ") then
				playSound('jump.ogg')
				self:jumpAction()
			end
		end

		-- WALKING ==> STOPPED
		if self.motion == WALKING then
			if self.direction == RIGHT and self.velocity.x <= 0 then
				self:walkStop()
			elseif self.direction == LEFT and self.velocity.x >= 0 then
				self:walkStop()
			end
		end

	end,

    onCollide = function (self, other, hOverlap, vOverlap)
		print("other:" .. tostring(other))
		print("top:" .. tostring(the.app.stackfleas.topSprite))
		if other:instanceOf(StackFlea) then
			if the.app.stackfleas:isTop(other) then
				if self.motion == JUMPING then
					self:jumpStop()
					the.app:clonePlayer()
					self:reinit()
				end
			end
		end
		print(string.format("ho %d, vo %d", hOverlap, vOverlap))
	end,


	walkLeft = function (self)
		self.direction = LEFT
		self.motion = WALKING
		self:play('walk_left')
		self.velocity.x = -200
		self.acceleration.x = 300
		self.paused = false
	end,

	walkRight = function (self)
		self.direction = RIGHT
		self.motion = WALKING
		self:play('walk_right')
		self.velocity.x = 200
		self.acceleration.x = -300
		self.paused = false
	end,

	walkStop = function (self)
		-- inherit previous direction...
		self.motion = STOPPED
		if self.direction == LEFT then
			self:play("pause_left")
		else
			self:play("pause_right")
		end
		self.acceleration.x = 0
		self.velocity.x = 0

		self.paused = true
	end,

	jumpStop = function (self)
		-- stop vertical movement
		self.acceleration.y = 0
		self.velocity.y = 0
		-- call walkStop()
		self:walkStop()
	end,

	jumpAction = function (self)
		-- inherit previous direction
		self.motion = JUMPING

		if self.direction == LEFT then
			self:play('jump_left')
		else
			self:play('jump_right')
		end

		-- we want to jump approx 5 times the flea height
		self.velocity.y = -250
		self.acceleration.y = 200

		-- neutralize decceleration so we have a 'resistance free' jump
		self.acceleration.x = 0
		-- but reduce the speed
		self.velocity.x = self.velocity.x / 3

		self.paused = false
	end,


}


